<?php js_validate();?>
<form id="form-validated" class="input" action="<?php echo $own_links;?>save" method="post">
		<input type="hidden" name="asuransi_id" id="asuransi_id" value="<?php echo isset($val->asuransi_id)?$val->asuransi_id:'';?>" />
		<table class="table-form" width="100%">
			<tr>
				<td>Nama Asuransi</td>
				<td><input type="text" id="asuransi_name" name="asuransi_name" class="validate[required]"size="50" value="<?php echo isset($val->asuransi_name)?$val->asuransi_name:'';?>" /></td>
			</tr>
			<tr>
				<td>Email Asuransi</td>
				<td><input type="text" id="asuransi_email" name="asuransi_email" class="validate[required]"size="30" value="<?php echo isset($val->asuransi_email)?$val->asuransi_email:'';?>" /></td>
			</tr>
			<tr valign="top">
				<td>Alamat Asuransi</td>
				<td><textarea id="asuransi_address" name="asuransi_address" cols="55" rows="3" class="validate[required]"><?php echo isset($val->asuransi_address)?$val->asuransi_address:'';?></textarea></td>
			</tr>
			<tr>
				<td colspan="2"><a href="#" id="add-jenis" class="west" title="Tambah Jenis Garansi"><img src="<?php echo themeUrl();?>images/add.png" /></a><i class="to">* klik gambar untuk menambahkan jenis garansi</i></td>
			</tr>
			<tr style="border:none;">
				<td colspan="2">
					<tbody id="jenis-items"></tbody>
				</td> 
			</tr>
			<tr><td colspan="2">&nbsp;</td></tr>
			<tr>
				<td><input type="reset" onclick="document.location='<?php echo $own_links;?>'" class="btnbatal" value="Batal" /></td>
				<td align="right"><input type="button" class="btnsimpan" id="btn-simpan" value="Simpan" /></td>
			</tr>
		</table>
	</form>

<script language="javascript">
jQuery('#add-jenis').click(function(){
	if(jQuery('.jenis_garansi').length !=0 ){
		if(jQuery.trim(jQuery('.jenis_garansi:last').val()) != ''){
			_add_item();
		}
	}else
		_add_item();
});

function _del_item(id){
	jQuery('#'+id).fadeOut("medium",function(){
		jQuery(this).remove();
	});
}

var jenis_opt = '<option value="">-- pilih jenis garansi --</option>';
<?php 
	if(count($jenis['data']) >0){
	  foreach($jenis['data'] as $r){?>
		jenis_opt +='<option value="<?php echo $r->garansi_key;?>"><?php echo $r->garansi_name;?></option>';
<?php } 
	}
?>

function _set_item(id){
	return '<tr id="'+id+'">'+
					'<td colspan="2">'+(jQuery('.jenis_garansi').length+1)+'. <select class="validate[required] jenis_garansi" onChange=_cek_jenis("jenis_garansi_'+id+'") id="jenis_garansi_'+id+'" name="jenis_garansi[]">'+jenis_opt+'</select><a href="#" onClick=_del_item("'+id+'")><img src="'+THEME_URL+'images/deleteo.png" /></a></td>'+
				'</tr>';
}
function _add_item(){
	rd=Math.floor(Math.random()*1000000);
	n_id = "id_"+rd;
	var items = _set_item(n_id);
	jQuery('#jenis-items').append(items);
	return false;
}
<?php if(isset($detail)){?>
var mmm = <?php echo json_encode($detail);?>;
_get_jenis_items(mmm);
function _get_jenis_items(o){
	jQuery('#jenis-items').html("");
	for(i=0;i<o.length;i++){
		mid = "id_"+o[i].detail_id;
		item_jenis = _set_item(mid);
		jQuery('#jenis-items').append(item_jenis);
		$('#jenis_garansi_'+mid).val(o[i].jenis);
	}
	
}
<?php } ?>

function _cek_jenis(m){
	val = $('#'+m).val();
	flag=0;
	$('.jenis_garansi').each(function(i){
		if($(this).val()==val)
			flag++;
	});	
	if(flag > 1){
		mBoxInfo("Jenis Garansi sudah ada di list");
		$('#'+m).val("");
	}
}

$('#btn-simpan').click(function(){
	if(jQuery('.jenis_garansi').length > 0){
		$('#form-validated').submit();
	}else{
		mBoxInfo("Jenis garansi tidak boleh kosong");
	}
});

</script>
